<?php
class BaseModelPDO extends BaseModelDBO {
	/**
	* The class constructor
	*/
	public function __construct($dbname) {
		$this->_dbName = $dbname;
	}
    /**
     * The class connector.
     *
     * @param string $host name of database host
     * @param string $user name of database user
     * @param string $passwd name of database password
     * @param string $dbname name of database name
     * @param unsigned integer $port name of database port
    */
	public function connector($host, $user, $passwd, $dbname, $port) {
		
		$this->_host = $host;
        $this->_user = $user;
        $this->_passwd = $passwd;
        $this->_dbName = $dbname;
        $this->_port = $port;
		
		$this->_setUniqueKey();
		
		$this->_key = $this->_getUniqueKey();
		
		if( NULL == self::$links[$this->_key] && !empty($this->_key) ) {
			$dsn = "mysql:dbname=".$this->getDataBaseName().";port=".$this->getPort().";host=".$this->getHost();
			self::$links[$this->_key] = @new PDO($dsn, $this->getUser(), $this->getPasswd());
			self::$links[$this->_key]->exec("set names 'gbk'");
			self::$links[$this->_key]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
		}
		$this->_link = self::$links[$this->_key];
	}
    /**
     * Get pdo object attributes by a query SQL.
     *
     * @param string $query name of sql query
     * @param reference array $attrs name of MYSQLI_RESULT object attributes
     * @return bool 
    */
	public function getAttributes($query, &$attrs=array()) {
		if( !$this->checkLink(SLAVE) ) {
			return FALSE;
		}
		$stmt = $this->_link->prepare($query);
		$stmt->execute();
		$stmt->setFetchMode(PDO::FETCH_ASSOC);
		return $attrs = $stmt->fetchAll();
	}
    /**
     * Execute an SQL query.
     *
     * @param string $query name of sql query 
     * @return MYSQLI_RESULT object 
    */
	public function runQuery($query) {
		if( !$this->checkLink(MASTER) ) {
			return FALSE;
		}
		$ret = $this->_link->exec($query);
		return $ret === 0 ? TRUE : FALSE;
	}
	/**
	 * last insert id 
	*/
	public function getInsertId() {
		if( !$this->checkLink(MASTER) ) {
			return FALSE;
		}
		return $this->_link->lastInsertId;
	}
	/**
	 * get affect rows
	*/
	public function getAffectedRows() {
		if( !$this->checkLink(SLAVE) ) {
			return FALSE;
		}
		return $this->_link->rowCount();
	}
	/**
	 * get one element
	*/
	public function getOneAttribute($query, &$val=NULL) {
		if( !$this->checkLink(SLAVE) ) {
			return FALSE;
		}
		$attrs = $this->getAttributes($query);
		return $val = $attrs[0];		
	}
	/**
	* Error sql information
	*/
	public function errorInfo() {
		if( !$this->checkLink(SLAVE) ) {
			return FALSE;
		}
		return $this->_link->errorInfo;
	}
	/**
	* Error sql code
	*/
	public function errorCode() {
		if( !$this->checkLink(SLAVE) ) {
			return FALSE;
		}
		return $this->_link->errorCode;
	}
	/**
	 * unset the current use pdo link
	 * 
	*/
	public function close() {
		unset(self::$links[$this->_key]);
	}
	/**
	 * unset all the opened pdo links	
	*/
	public function multiUnset() {
		self::$links = array();
	}
	
}
?>
